package leetcode;

import java.util.Stack;

public class erbaiershiqi {
    public static int calculate(String s) {
        //先进行预处理,去掉空格
        s = s.replaceAll(" ", "");
        //判断第一个是不是数字
        if(Character.isDigit(s.charAt(0))){
            s="+"+s;
        }
        Stack<Integer> stack=new Stack<>();
        char op=' ';
        for(int i=0;i<s.length();i++){
            if(s.charAt(i)=='+'||s.charAt(i)=='-'||s.charAt(i)=='*'||s.charAt(i)=='/'){
                op=s.charAt(i);
                continue;
            }
            int tmp=0;
            while(i<s.length()&&Character.isDigit(s.charAt(i))){
                tmp=tmp*10+(s.charAt(i)-'0');
                i++;
            }
            i--;
            if(op=='+'){
                stack.push(tmp);
            }
            if(op=='-'){
                stack.push(-tmp);
            }
            if(op=='*'){
                int m=stack.pop();
                int n=m*tmp;
                stack.push(n);
            }
            if(op=='/'){
                int p=stack.pop();
                int y=p/tmp;
                stack.push(y);
            }

        }
        int sum=0;
        while(!stack.isEmpty()){
            sum=sum+stack.pop();
        }
        return  sum;
    }

    public static void main(String[] args) {
        String s=" 3/2 ";
        int calculate = calculate(s);
        System.out.println(calculate);

    }
}
